package com.fg.pro.ai.AiController;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ToolConfig {

    @Autowired
    DashScopeChatModel dashScopeChatModel;

    @Autowired
    ToolService toolService;

    @Bean
    ChatClient toolClient(ChatMemory toolChatMemory) {
        ChatClient chatClient = ChatClient
                .builder(dashScopeChatModel)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(toolChatMemory)
                                .conversationId("12222123") // 在builder中设置
                                .build()
                )
                .defaultTools(
                        toolService
                )
                .build();
        return chatClient;
    }


    @Bean
    ChatMemory toolChatMemory(JdbcChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory
                .builder()
                .maxMessages(3)
                .chatMemoryRepository(chatMemoryRepository)
                .build();
    }



}
